package com.dc.web.admin;

import com.dc.po.Type;
import com.dc.service.CommentService;
import com.dc.service.Impl.CommentServiceImpl;
import com.dc.service.Impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;


@Controller
@RequestMapping("/admin")
public class CommentAdminController {

    @Autowired
    private CommentServiceImpl commentService;

    //前往评论列表
    @GetMapping("/comments") //配置分页数据参数  每页15条，create_time倒序排列
    public String types(@PageableDefault(size = 15,sort = {"createTime"},direction = Sort.Direction.DESC)
                                Pageable pageable, Model model){
        model.addAttribute("page",commentService.listComment(pageable));
        return "admin/comments";
    }

    @GetMapping("/comments/{id}/delete")
    public String delete(@PathVariable Long id,
                         RedirectAttributes attributes){
        commentService.deleteComment(id);
        attributes.addFlashAttribute("commentMsg","删除评论成功");
        return "redirect:/admin/comments";
    }

}
